<!-- pages/search/search.vue -->
<template>
  <view class="search-container">
    <!-- 搜索框 -->
    <view class="search-box">
      <uni-icons type="arrow-left" size="20" color="#999" @click="goBack"></uni-icons>
      <view class="input-wrapper">
        <uni-icons type="search" size="16" color="#999"></uni-icons>
        <input 
          type="text" 
          placeholder="请输入搜索关键词" 
          v-model="searchText"
          @confirm="handleSearch"
          focus
        />
      </view>
      <text class="cancel" @click="goBack">取消</text>
    </view>

    <!-- 热门搜索 -->
    <view class="hot-search" v-if="showHotSearch">
      <view class="title">热门搜索</view>
      <view class="tags">
        <text 
          class="tag" 
          v-for="(item, index) in hotKeywords" 
          :key="index"
          @click="searchByKeyword(item)"
        >
          {{item}}
        </text>
      </view>
    </view>

    <!-- 搜索结果 -->
    <view class="search-result" v-else>
      <view class="result-count">找到{{searchResults.length}}个结果</view>
      <view class="result-list">
        <view 
          class="result-item" 
          v-for="(item, index) in searchResults" 
          :key="index"
          @click="goToProduct(item)"
        >
          <text class="name">{{item.name}}</text>
          <text class="price">¥{{item.price}}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      searchText: '',
      showHotSearch: true,
      hotKeywords: [
        '莲花清瘟胶囊', '口罩', '鸡蛋', 
        '儿童口罩', '酒精', '姜', 
        '蒜', '药', '娃娃菜', '葱', '菠菜'
      ],
      searchResults: []
    }
  },
  methods: {
    goBack() {
      uni.navigateBack()
    },
    searchByKeyword(keyword) {
      this.searchText = keyword
      this.handleSearch()
    },
    async handleSearch() {
      if (!this.searchText.trim()) {
        uni.showToast({
          title: '请输入搜索内容',
          icon: 'none'
        })
        return
      }
      
      try {
        // 模拟搜索请求
        const res = await uni.request({
          url: 'http://localhost:3000/search',
          data: {
            keyword: this.searchText
          }
        })
        
        if (res.statusCode === 200) {
          this.searchResults = res.data
          this.showHotSearch = false
        }
      } catch (error) {
        console.error('搜索失败:', error)
        uni.showToast({
          title: '搜索失败',
          icon: 'none'
        })
      }
    },
    goToProduct(product) {
      uni.navigateTo({
        url: `/pages/shop/shop-detail?id=${product.id}`
      })
    }
  }
}
</script>

<style lang="scss">
.search-container {
  padding: 20rpx;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.search-box {
  display: flex;
  align-items: center;
  margin-bottom: 30rpx;
  
  .input-wrapper {
    flex: 1;
    background-color: #fff;
    border-radius: 30rpx;
    display: flex;
    align-items: center;
    padding: 10rpx 20rpx;
    margin: 0 20rpx;
    
    input {
      flex: 1;
      font-size: 28rpx;
      margin-left: 10rpx;
      color: #333;
    }
  }
  
  .cancel {
    font-size: 28rpx;
    color: #666;
  }
}

.hot-search {
  .title {
    font-size: 28rpx;
    color: #999;
    margin-bottom: 20rpx;
  }
  
  .tags {
    display: flex;
    flex-wrap: wrap;
    
    .tag {
      background-color: #fff;
      padding: 10rpx 20rpx;
      border-radius: 30rpx;
      margin-right: 20rpx;
      margin-bottom: 20rpx;
      font-size: 26rpx;
      color: #333;
    }
  }
}

.search-result {
  background-color: #fff;
  border-radius: 10rpx;
  padding: 20rpx;
  
  .result-count {
    font-size: 26rpx;
    color: #999;
    margin-bottom: 20rpx;
  }
  
  .result-item {
    display: flex;
    justify-content: space-between;
    padding: 20rpx 0;
    border-bottom: 1rpx solid #eee;
    
    .name {
      font-size: 28rpx;
      color: #333;
    }
    
    .price {
      font-size: 28rpx;
      color: #ff6b6b;
      font-weight: bold;
    }
  }
}
</style>